package com.cr.front.topic.service.impl;

import com.cr.common.response.PageData;
import com.cr.front.topic.dao.mapper.TopicMapper;
import com.cr.front.topic.dao.repository.TopicRepository;
import com.cr.front.topic.pojo.po.TopicPO;
import com.cr.front.topic.pojo.query.TopicQuery;
import com.cr.front.topic.pojo.vo.TopicVO;
import com.cr.front.topic.service.TopicService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    private TopicRepository topicRepository;
    @Override
    public PageData<TopicVO> selectList(TopicQuery topicQuery) {
        List<TopicPO> topicPOList = topicRepository.selectList(topicQuery);
        List<TopicVO> topicVOList = BuildTopicVOList(topicPOList);
        Long count = topicRepository.selectCount(topicQuery);
        return new PageData<>(count,topicVOList);
    }

    @Override
    public TopicVO selectById(Long id) {
        TopicPO topicPO = topicRepository.selectById(id);
        TopicVO topicVO=new TopicVO();
        if(topicPO!=null){
            BeanUtils.copyProperties(topicPO,topicVO);
            return topicVO;
        }
        return null;
    }

    private List<TopicVO> BuildTopicVOList(List<TopicPO> topicPOS) {
        List<TopicVO> topicVOList = new ArrayList<>();
        for(TopicPO topicPO:topicPOS){
            TopicVO topicVO=new TopicVO();
            BeanUtils.copyProperties(topicPO,topicVO);
            topicVOList.add(topicVO);
        }
        return topicVOList;

    }
}
